﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UberChess.Structure.Pieces;

namespace UberChess.Searching.Transposition
{
   [Serializable]
   public class TranspositionTable
   {
      private HashSet<State> table;

      public TranspositionTable()
      {
         table = new HashSet<State>();
      }

      public int Count { get { return table.Count; } }

      public void AddState(State state)
      {
         try
         {
            table.Add(state);
         }
         catch (Exception)
         {
            
         }
      }

      public bool Contains(State state)
      {
         //if (Table.Any(currentState => IsEqual(currentState, state)))
         //{
         //   return true;
         //}
         //else
         //{
         //   return false;
         //}

         return table.Contains(state);
      }

      private bool IsEqual(State currentState, State state)
      {
         for (int i = 0; i < state.state.GetLength(0); i++)
         {
            for (int j = 0; j < state.state.GetLength(1); j++)
            {
            
               
               IPiece currentPiece = currentState.state[i, j];
               IPiece comparingPiece = state.state[i, j];

               if (currentPiece.GetType() == typeof(Empty) && comparingPiece.GetType() == typeof(Empty)) continue;
               if (currentPiece.GetType() == typeof(Empty) || comparingPiece.GetType() == typeof(Empty)) return false;

               if (!(currentPiece.GetSide() == comparingPiece.GetSide() &&
                  currentPiece.GetType() == comparingPiece.GetType()))
               {
                  return false;
               }
            }
         }
         return true;
      }

      public void Clear()
      {

         table.Clear();

      }
   }
}
